#include "stdafx.h"
#include "NetJob.h"

CNetJob::CNetJob()
{
	m_pBuf = NULL;
	m_nTotal = 0;
	m_nLen = 0;
	m_nErrType = ErrorType_None;
}

CNetJob::~CNetJob()
{
	if (m_pBuf)
	{
		delete m_pBuf;
		m_pBuf = NULL;
	}
}

void CNetJob::OnContentEncoding(const string& szCoding)
{
	m_szCoding = szCoding;
}

void CNetJob::OnContentLength(int nLen)
{
	m_nTotal = nLen;
}


void CNetJob::OnRecv(char* pBuf,int nLen)
{
	if (nLen > 0)
	{
		int nNew = m_nLen + nLen;
		char* pNew = new char[nNew];
		memset(pNew,0,nNew);
		memcpy(pNew,m_pBuf,m_nLen);
		memcpy(pNew+m_nLen,pBuf,nLen);

		if (m_pBuf)
		{
			delete m_pBuf;
		}

		m_pBuf = pNew;
		m_nLen = nNew;

		/*if (m_pNetJobMsg)
		{
			m_pNetJobMsg->NetOnRecv(this,nLen);
		}*/

	}
	



	/*if (0 == m_nTotal)
	{
		m_nTotal = nLen*2;
		m_pBuf = new char[m_nTotal];
		memset(m_pBuf,0,m_nTotal);

		memcpy(m_pBuf,pBuf,nLen);
		m_nLen = nLen;
	}
	else if (m_nTotal - m_nLen >= nLen)
	{
		memcpy(m_pBuf,pBuf,nLen);
		m_nLen+= nLen;
	}
	else
	{
		int nNewTotal = m_nLen + nLen*2;
		char * pNew = new char[nNewTotal];
		memset(pNew,0,nNewTotal);
		memcpy(pNew,m_pBuf,m_nLen);
		memcpy(pNew+m_nLen,pBuf,nLen);
		m_nLen += nLen;

		delete m_pBuf;
		m_pBuf = pNew;


		m_nTotal = nNewTotal;
		
	}*/
}


void CNetJob::OnError(ErrorType nType)
{
	m_nErrType = nType;
	if (nType == ErrorType_Finish)
	{
		if (m_pNetJobMsg)
		{
			m_pNetJobMsg->NetJobFinish(this);
		}
	}
	else
	{
		if (m_pNetJobMsg)
		{
			m_pNetJobMsg->NetJobError(this);
		}
	}
}

void CNetJob::Init(INetJobMsg* pNetJobMsg)
{
	m_pNetJobMsg = pNetJobMsg;
}

//string CNetJob::GetBufString()
//{
//	return string(m_pBuf,m_nLen);
//}

CNetFileJob::CNetFileJob(LONGLONG nHasRecv,LONGLONG nFileSize)
{
	m_pNetJobMsg = NULL;
	m_nErrType = ErrorType_None;
	m_nHasRecv = nHasRecv;
	m_nFileSize = nFileSize;
}

CNetFileJob::~CNetFileJob()
{
	
}

void CNetFileJob::OnContentEncoding(const string& szCoding)
{
	m_szCoding = szCoding;
}

void CNetFileJob::OnContentRange(const string& szRange)
{
	int nPos = szRange.find('/');
	if (nPos>0)
	{
		string szSize= szRange.substr(nPos+1,szRange.size() - nPos-1);
		m_nFileSize = atoi(szSize.c_str());
	}
}

void CNetFileJob::OnError(ErrorType nType)
{
	m_nErrType = nType;
	if (nType == ErrorType_Finish && m_nHasRecv != 0 &&m_nTotal == m_nHasRecv)
	{
		if (m_pNetJobMsg)
		{
			m_pNetJobMsg->NetJobFinish(this);
		}
	}
	else
	{
		if (m_pNetJobMsg)
		{
			m_pNetJobMsg->NetJobError(this);
		}
	}
}

bool CNetFileJob::Init(INetJobMsg* pNetJobMsg,const CString& strFilePath,bool bAppend )
{
	m_pNetJobMsg = pNetJobMsg;
	m_strFilePath = strFilePath;
	try
	{
		if (bAppend)
		{
			m_File.Open(strFilePath,CFile::modeCreate|CFile::modeReadWrite|CFile::shareDenyNone|CFile::modeNoTruncate);
			m_File.SeekToEnd();
		}
		else
		{
			m_File.Open(strFilePath,CFile::modeCreate|CFile::modeReadWrite|CFile::shareDenyNone);
		}

		return true;
	}
	
	catch (CException* e)
	{
		TCHAR err[255]={0};
		e->GetErrorMessage(err,255);
		OutputDebugString(err);
	}
	return false;
}

void CNetFileJob::OnRecv(char* pBuf,int nLen)
{
	try
	{
		m_File.Write(pBuf,nLen);
		m_File.Flush();

		m_nHasRecv += nLen;
		if (m_pNetJobMsg)
		{
			if (0 == m_nFileSize)
			{
				m_nFileSize = m_nTotal;
			}
			m_pNetJobMsg->NetOnRecv(this,m_nHasRecv,m_nFileSize);
		}

		if (m_nTotal == m_nHasRecv)
		{
			m_File.Close();
		}

	}
	catch (CException* e)
	{
		TCHAR err[255]={0};
		e->GetErrorMessage(err,255);
		OutputDebugString(err);
	}


}


void CNetUploadFile::OnUploadError(ErrorType nType)
{
	m_nErrType = nType;
	if (nType == ErrorType_Finish )
	{
		if (m_pNetJobMsg)
		{
			m_pNetJobMsg->NetJobFinish(this);
		}
	}
	else
	{
		if (m_pNetJobMsg)
		{
			m_pNetJobMsg->NetJobError(this);
		}
	}
}

void CNetUploadFile::OnFileSend(UploadHttpTaskData *pData)
{
	if (m_pNetJobMsg)
	{
		m_pNetJobMsg->NetOnFileSend(this,pData->nFileHasSended,pData->nFileSize);
	}
}

bool CNetUploadFile::Init(INetJobMsg* pNetJobMsg,const CString& strFilePath)
{
	m_pNetJobMsg = pNetJobMsg;
	m_strFilePath = strFilePath;

	return true;
}

void CNetUploadFile::OnReturn(char* pBuf,int nLen)
{
	CNetJob::OnRecv(pBuf,nLen);
}
